﻿using AutoMapper;
using ChikovMF.Application.Features.Projects.CreateProject;
using ChikovMF.Application.Features.Projects.DeleteProject;
using ChikovMF.Application.Features.Projects.DetailProject;
using ChikovMF.Application.Features.Projects.EditProject;
using ChikovMF.Application.Features.Projects.GetListProject;
using ChikovMF.Application.Features.Projects.UploadImagesCommand;
using ChikovMF.WebAPI.Models.Projects;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace ChikovMF.WebAPI.Controllers
{
    [Route("api/Projects")]
    [ApiController]
    public class ProjectController : ControllerBase
    {
        [HttpGet]
        public async Task<ActionResult<ListProjectModel>> List()
        {
            var query = new ListProjectQuery();
            var viewModel = await _mediator.Send(query);
            return Ok(viewModel);
        }

        [HttpPost, Authorize]
        public  async Task<ActionResult<Guid>> Create(ProjectDto createProjectModel)
        {
            var command = _mapper.Map<CreateProjectCommand>(createProjectModel);
            var projectId = await _mediator.Send(command);
            return Ok(projectId);
        }

        [HttpPost("UploadImages/{projectId:guid}"), Authorize]
        public async Task UploadImages(Guid projectId, [FromForm] UploadProjectImagesDto imagesDto)
        {
            var command = _mapper.Map<UploadImagesCommand>(imagesDto);
            command.ProjectId = projectId;
            await _mediator.Send(command);
        }

        [HttpDelete("{projectId:guid}"), Authorize]
        public async Task Delete(Guid projectId)
        {
            var command = new DeleteProjectCommand
            {
                ProjectId = projectId
            };
            await _mediator.Send(command);
        }

        [HttpGet("{projectId:guid}")]
        public async Task<ActionResult<DetailProjectModel>> Detail(Guid projectId)
        {
            var query = new DetailProjectQuery
            {
                ProjectId = projectId
            };
            var viewModel = await _mediator.Send(query);

            return Ok(viewModel);
        }

        [HttpGet("Edit/{projectId:guid}"), Authorize]
        public async Task<ActionResult<EditProjectModel>> GetEdit(Guid projectId)
        {
            var query = new EditProjectQuery
            {
                ProjectId = projectId
            };
            var viewModel = await _mediator.Send(query);

            return Ok(viewModel);
        }

        [HttpPut("{projectId:guid}"), Authorize]
        public async Task<ActionResult<Guid>> Edit(Guid projectId, ProjectDto editProjectModel)
        {
            var command = _mapper.Map<EditProjectCommand>(editProjectModel);
            command.ProjectId = projectId;

            projectId = await _mediator.Send(command);
            return Ok(projectId);
        }

        private readonly IMediator _mediator;
        private readonly IMapper _mapper;

        public ProjectController(IMediator mediator, IMapper mapper)
        {
            _mediator = mediator;
            _mapper = mapper;
        }
    }
}
